package cn.patient.pp.module.knowledge.service;

import cn.patient.pp.framework.common.pojo.PageResult;
import cn.patient.pp.framework.common.util.date.DateUtils;
import cn.patient.pp.framework.common.util.object.BeanUtils;
import cn.patient.pp.module.knowledge.controller.admin.vo.KnowledgeBasePageReqVO;
import cn.patient.pp.module.knowledge.controller.admin.vo.KnowledgeBaseRespVO;
import cn.patient.pp.module.knowledge.controller.admin.vo.KnowledgeBaseSaveReqVO;
import cn.patient.pp.module.knowledge.dal.dataobject.KnowledgeBaseDO;
import cn.patient.pp.module.knowledge.dal.mysql.KnowledgeBaseMapper;
import cn.patient.pp.module.patients.framework.web.config.LogRecordConstants;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import static cn.patient.pp.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.patient.pp.module.infra.enums.ErrorCodeConstants.KNOWLEDGE_BASE_NOT_EXISTS;

/**
 * @author liqingsong
 * @version 1.0
 * Create by 2025/4/14
 */

@Service
@Validated
public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {

    @Resource
    private KnowledgeBaseMapper knowledgeBaseMapper;

    @Override
    @LogRecord(success = LogRecordConstants.KONWLEDGE_CREATE_SUCCESS,
            operator = "{{#currentUser}}",
            type = LogRecordConstants.KONWLEDGE,
            fail = "创建知识库失败，失败原因：「{{#_errorMsg}}」",
            subType = LogRecordConstants.KONWLEDGE_CREATE ,bizNo = LogRecordConstants.KONWLEDGE_CREATE_BIZ_NO)
    public Integer createKnowledgeBase(KnowledgeBaseSaveReqVO createReqVO) {
        // 插入
        KnowledgeBaseDO knowledgeBase = BeanUtils.toBean(createReqVO, KnowledgeBaseDO.class);
        knowledgeBaseMapper.insert(knowledgeBase);
        // 返回
        return knowledgeBase.getId();
    }

    @Override
    @LogRecord(success = LogRecordConstants.KONWLEDGE_UPDATE_SUCCESS,
            operator = "{{#currentUser}}",
            type = LogRecordConstants.KONWLEDGE,
            fail = "更新知识库失败，失败原因：「{{#_errorMsg}}」",
            subType = LogRecordConstants.KONWLEDGE_UPDATE ,bizNo = "{{#updateReqVO.id}}")
    public void updateKnowledgeBase(KnowledgeBaseSaveReqVO updateReqVO) {
        // 校验存在
        validateKnowledgeBaseExists(updateReqVO.getId());
        // 更新
        KnowledgeBaseDO updateObj = BeanUtils.toBean(updateReqVO, KnowledgeBaseDO.class);
        knowledgeBaseMapper.updateById(updateObj);
    }

    @Override
    @LogRecord(success = LogRecordConstants.KONWLEDGE_DELETE_SUCCESS,
            operator = "{{#currentUser}}",
            type = LogRecordConstants.KONWLEDGE,
            fail = "删除知识库失败，失败原因：「{{#_errorMsg}}」",
            subType = LogRecordConstants.KONWLEDGE_DELETE ,bizNo = "{{#id}}")
    public void deleteKnowledgeBase(Integer id) {
        // 校验存在
        validateKnowledgeBaseExists(id);
        // 删除
        knowledgeBaseMapper.deleteById(id);
    }

    private void validateKnowledgeBaseExists(Integer id) {
        if (knowledgeBaseMapper.selectById(id) == null) {
            throw exception(KNOWLEDGE_BASE_NOT_EXISTS);
        }
    }

    @Override
    @LogRecord(success = LogRecordConstants.KONWLEDGE_DETAIL_SUCCESS,
            operator = "{{#currentUser}}",
            type = LogRecordConstants.KONWLEDGE,
            fail = "查看知识库详情失败，失败原因：「{{#_errorMsg}}」",
            subType = LogRecordConstants.KONWLEDGE_DETAIL ,bizNo = "{{#id}}")
    public KnowledgeBaseDO getKnowledgeBase(Integer id) {
        return knowledgeBaseMapper.selectById(id);
    }

    @Override
    @LogRecord(success = LogRecordConstants.KONWLEDGE_PAGE_SUCCESS,
            operator = "{{#currentUser}}",
            type = LogRecordConstants.KONWLEDGE,
            fail = "查看知识库分页失败，失败原因：「{{#_errorMsg}}」",
            subType = LogRecordConstants.KONWLEDGE_PAGE ,bizNo = LogRecordConstants.KONWLEDGE_PAGE_BIZ_NO)
    public PageResult<KnowledgeBaseRespVO> getKnowledgeBasePage(KnowledgeBasePageReqVO pageReqVO) {
        PageResult<KnowledgeBaseDO> knowledgeBaseDOPageResult = knowledgeBaseMapper.selectPage(pageReqVO);
        PageResult<KnowledgeBaseRespVO> KnowledgeBaseRespVO = BeanUtils.toBean(knowledgeBaseDOPageResult, KnowledgeBaseRespVO.class);
        // 遍历结果集，设置状态
        KnowledgeBaseRespVO.getList().forEach(knowledgeBaseDO -> {
            LocalDate currentDate = LocalDate.now();
            if (knowledgeBaseDO.getAbortDate()!=null){
                //转换日期
                LocalDate abortDate = LocalDate.parse(knowledgeBaseDO.getAbortDate());
                if (currentDate.isAfter(abortDate)) {
                    //废止
                    knowledgeBaseDO.setStatus("2");
                }else {
                    //现行
                    knowledgeBaseDO.setStatus("1");
                }
            }else {
                //现行
                knowledgeBaseDO.setStatus("1");
            }
        });
        return KnowledgeBaseRespVO;
    }
}

